package com.example.frameboot.common;


import com.alibaba.excel.util.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author xuzh10@centaline.com.cn
 * @date 2020/5/20 13:52
 */
@Component
public class SpringBeanUtil implements ApplicationContextAware {

    protected static ApplicationContext context;

    public static Object getBean(String name) {
        //name表示其他要注入的注解name名
        try {
            if (context != null && StringUtils.isNotBlank(name)) {
                return context.getBean(name);
            }
        } catch (Exception ex) {

        }
        return null;
    }

    public static <T> T getBean(Class<T> obj) {
        try {
            if (context != null) {
                return context.getBean(obj);
            }
        } catch (Exception ex) {

        }
        return null;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }
}
